﻿#region Copyright
// Copyright (c) 2011-2012 RaisingForce Team (Alikin Sergey)
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System;
using System.Diagnostics.Contracts;
using Aiemp.ObjectModel;
using RaisingForce.Raise.Hardware;

namespace RaisingForce.Raise.Presentation.ViewModel.Specialized
{
    public abstract class HardwareSettingViewModelBase :
        ViewModelBase,
        IHardwareSettingViewModel
    {
        #region Constructors

        protected HardwareSettingViewModelBase(RegistryKey key)
            : base()
        {
            // Contract.Requires(RaiseRegistry.Hardware.ContainsKey(key));
            // ObjectRegistry.ContainsKey method should be marked as Pure for using in contract!
            this.registryItem = RaiseRegistry.Hardware[key];
            Contract.Assume(this.registryItem != null);
            this.setupLocalizedNameAndDescription();
        }

        protected HardwareSettingViewModelBase(
            Func<string> getName,
            Func<string> getDescription)
        {   // contructor creates fake setting view model
            // this.registryItem = null; - by default
            if (getName == null)
            {
                throw new ArgumentNullException("getName");
            }
            if (getDescription == null)
            {
                throw new ArgumentNullException("getDescription");
            }
            Contract.EndContractBlock();
            this.getName = getName;
            this.getDescription = getDescription;
            this.setupLocalizedNameAndDescription();
        }

        #endregion

        #region Private Declarations

        private readonly RobotHardwareRegistryItem registryItem;

        private readonly Func<string> getName;

        private readonly Func<string> getDescription;

        private string name;

        private string description;

        [ContractInvariantMethod]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "Required for code contracts.")]
        private void ObjectInvariant()
        {
            Contract.Invariant(this.registryItem != null
                || this.getName != null && this.getDescription != null);
        }

        private void setupLocalizedNameAndDescription()
        {
            if (this.registryItem != null)
            {
                // Create hardware instance to get localized items
                IRobotHardware hardware = this.registryItem.CreateInstance<IRobotHardware>();
                // Get localized hardware Name
                string hardwareName = hardware.Name;
                this.name = string.IsNullOrEmpty(hardwareName) ?
                    this.registryItem.Name :
                    hardwareName;
                // Get localized hardware Description
                string hardwareDescription = hardware.Description;
                this.description = string.IsNullOrEmpty(hardwareDescription) ?
                    this.registryItem.Description :
                    hardwareDescription;
            }
            else
            {   // this view model is fake
                this.name = this.getName();
                this.description = this.getDescription();
            }
        }

        #endregion

        #region ViewModel Properties

        private const string propName = "Name";

        public string Name
        {
            get { return this.name; }
        }

        private const string propDescription = "Description";

        public string Description
        {
            get { return this.description; }
        }

        public RegistryKey Key
        {
            get
            {
                return this.registryItem != null ?
                    this.registryItem.Key
                    : default(RegistryKey);
            }
        }

        #endregion

        #region ILocalizable Members

        public void UpdateLocalization()
        {
            this.setupLocalizedNameAndDescription();
            this.OnPropertyChanged(propName);
            this.OnPropertyChanged(propDescription);
        }

        #endregion

        #region IHardwareSettingViewModel Members

        protected abstract void SetAsDefaultHardwareImplementation(
            RobotHardwareRegistryItem registryItem);

        public void SetAsDefaultHardware()
        {
            if (this.registryItem != null)
            {
                this.SetAsDefaultHardwareImplementation(this.registryItem);
            }
        }

        #endregion
    }
}
